// pages/currentTargets/currentTargets.js
var app = getApp()
var admin_id = app.globalData.admin_id;
var goods_id = 0;
var WxParse = require('../../wxParse/wxParse.js');
var share = app.globalData.share;
var key = 0;
Page({

	data: {
		spe_name: '',
    isc:false
	},

	onLoad: function (options) {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/api/goods/goodsInfo?admin_id=' + admin_id + '&type=' + options.type + '&goods_id=' + options.id + '&spec=1',
			success: function (e) {
				console.log(e)
				that.setData({
					items: e.data.data[0],
					spc: e.data.spec
				})

				var article = e.data.data[0].goods_content;
				WxParse.wxParse('article', 'html', article, that, 8);

				var arr = [];
				var arrs = [];
				var arrss = [];
				for (var i in e.data.spec.filter_spec) {
					// console.log(e.data.spec.filter_spec[i])
					arr.push(e.data.spec.filter_spec[i])
					arrss.push(0)
				}

				for (var i in e.data.spec.spec_goods_price) {
					// console.log(e.data.spec.spec_goods_price[i])
					arrs.push(e.data.spec.spec_goods_price[i])
				}

				that.setData({
					spec: arr,
					spec_price: arrs,
					curIndexs: arrss
				})
				that.setSpe()
        that.getcdata()
			}
		})
    
		app.getIntB(function () {
			that.setData({
				intB: parseInt(app.globalData.intB),
				sw_int: app.globalData.sw_int
			})
		})

		app.checkUser(app.globalData.openid)
	},
	onShareAppMessage: function () {
		var that = this;
		return {
			title: '这件商品很不错哦！',
			path: 'pages/goods/goods?id=' + that.data.items.goods_id + '&type=' + that.data.items.cat_id,
			success: function () {
				if (that.click_limit(admin_id)) {
					app.opInt(1, 'integral', 1, function () {
						// app.checkUser(app.globalData.openid, that)
						app.showPop('积分 +1', true)
					})
				}
			}
		}
	},
	filterHeaderGoodsid: function (e) {
		return (e.goods_id == goods_id)
	},
	submit: function (e) {
		var that = this;
		console.log(e.detail.value)
		if (e.detail.value.name != '' && e.detail.value.name != '') {
			app.globalData.tell = e.detail.value.tell;
			app.globalData.name = e.detail.value.name;
			wx.showToast({
				title: '成功,再次操作',
				success: function () {
					setTimeout(function () {
						that.setData({
							mask: false
						})
					}, 1000)
				}
			})
		} else {
			wx.showToast({
				title: '请填写信息',
			})
		}
	},
	close: function (e) {
		this.setData({
			mask: false
		})
	},
	buy: function (e) {
		if (app.globalData.tell != '') {
			this.pay(e)
		} else {
			this.setData({
				mask: true
			})
		}
	},
	pay: function (e) {
		console.log(e.currentTarget.dataset.price)
		var that = this
		// wx.chooseAddress({
		//     success: function (r) {
		// var name = r.userName;
		wx.request({
			url: app.globalData.host + 'index.php/api/goods/zhifus?name=' + app.globalData.name + '&sj=' + app.globalData.tell + '&openid=' + app.globalData.openid + '&price=' + e.currentTarget.dataset.price + '&goods_name=' + e.currentTarget.dataset.name + '&mch_id=' + app.globalData.mch + '&appid=' + app.globalData.appid,
			data: {},
			method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
			// header: {}, // 设置请求的 header
			success: function (res) {
				console.log('rs====' + e.currentTarget.dataset.price);
				console.log('支付====' + e.currentTarget.dataset.name);

				wx.requestPayment(
					{
						'appId': app.globalData.appid,
						'timeStamp': res.data.timeStamp,
						'nonceStr': res.data.nonceStr,
						'package': 'prepay_id=' + res.data.prepayid,
						'signType': 'MD5',
						'paySign': res.data.sign,
						'success': function (res) {
							wx.request({
								url: app.globalData.host + 'index.php/api/goods/order1',
								method: 'post',
								data: {
									'name': app.globalData.name,
									'sj': app.globalData.tell,
									'goods_name': '【订单】' + e.currentTarget.dataset.name,
									'price': e.currentTarget.dataset.price,
									// 'liuyan': e.currentTarget.dataset.liuyan,
									// 'dz': r.provinceName + r.cityName + r.countyName + r.detailInfo,
									'admin_id': admin_id,
									'goods_id': e.currentTarget.dataset.id
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded'
								},
								success: function (res) {
									console.log(res.data)
									that.setData({
										list: res.data[0],
									})
									setTimeout(function () {
										that.setData({
											loadingHidden: true
										})
									}, 1500)
								}
							})
						},
						'fail': function (res) {
							console.log('失败！' + res.errMsg)
						}
					})
			},
			fail: function () {
			},
			complete: function () {
			}
		})
		//     }
		// })
	},
	checkSpe: function (e) {
		console.log(e.currentTarget.dataset.value)
		var arr = this.data.curIndexs
		arr[e.currentTarget.dataset.idx] = e.currentTarget.dataset.index
		this.setData({
			curIndexs: arr,
		})
		this.setSpe()
	},
	setSpe: function () {
		if (this.data.spec.length <= 0) {
			return false
		}
		var value = '';
		var name = '';
		var arr = this.data.spec_price;
		var items = this.data.items;
		for (var i = 0; i < this.data.curIndexs.length; i++) {
			key = this.data.spec[i].item[this.data.curIndexs[i]].val
			arr = arr.filter(this.filterHeaderValue)
			name += (this.data.spec[i].item[this.data.curIndexs[i]].item + ' ')
			value += (this.data.spec[i].item[this.data.curIndexs[i]].val + ' ')
		}

		items.shop_price = arr[0].price
		this.setData({
			selectSpe: arr[0],
			items: items,
			spe_name: name,
			spe_value: value
		})

		// console.log(arr)
	},
	filterHeaderValue: function (e) {
		return (e.key.indexOf(key) >= 0)
	},
	topay: function () {
		var that = this;
		if (that.data.items.shop_price == '0.00') {
			wx.showModal({
				title: '提示',
				content: '价格为0的商品,不支持售卖哦',
			})
			return false;
		}
		app.checkUser(app.globalData.openid, that, function () {
			wx.reLaunch({
				url: '../../pages/reg/reg',
			})
		})
		var arr = [];
		var obj = this.data.items;
		obj.spe = this.data.spe_name;
		obj.num = 1;
		arr.push(obj)
		app.globalData.goods_info = arr;
		wx.navigateTo({
			url: '../../pages/pay/pay',
		})
	},
	toCart: function () {
		var that = this;
		wx.request({
			url: 'https://xcxu.we-fs.com/index.php/api/goods/cart?admin_id=' + app.globalData.admin_id + '&goods_id=' + that.data.items.goods_id + '&openid=' + app.globalData.openid + '&spec_key_name=' + that.data.spe_name + '&spec_key=' + that.data.spe_value + '&goods_price=' + that.data.items.shop_price,
			success: function (e) {
				console.log(e)
				app.showPop(e.data.msg, true)
			}
		})
	},
	click_limit: function (user_id, num) {
		if (!num) {
			num = 3
		}
		var myDate = new Date();
		myDate.getFullYear(); //获取完整的年份(4位,1970-????)
		myDate.getMonth(); //获取当前月份(0-11,0代表1月)
		myDate.getDate(); //获取当前日(1-31)
		var date = myDate.getFullYear() + '-' + myDate.getMonth() + '-' + myDate.getDate();
		//读取缓存
		var value = wx.getStorageSync('gru_click_limit');
		console.log(value)
		//首次没读到 设置指并 存入 return true
		if (!value) {
			value = {
				date: date,
				dos: [{
					times: 1,
					id: user_id
				}]
			};
			wx.setStorageSync('gru_click_limit', value);
			return true;
		}
		/*有值
		判断日期
		*/
		if (value.date == date) {
			//当天
			var is_have = false;
			for (var p in value.dos) {
				if (value.dos[p].id == user_id) {
					is_have = true;
					if (value.dos[p].times >= num) {
						return false;
					} else {
						value.dos[p].times++;
						wx.setStorageSync('gru_click_limit', value);
						return true;
						break;
					}
				}
			}
			if (is_have == false) {
				value.dos.push({
					times: 1,
					id: user_id
				})
				wx.setStorageSync('gru_click_limit', value);
				return true;
			}

		} else {
			//新的一天
			value.date = date;
			value.dos = [{
				times: 1,
				id: user_id
			}];
		}
		wx.setStorageSync('gru_click_limit', value);
		return true;
	},
  toCartpage(){
    wx.switchTab({
      url: '../../pages/cart/cart',
    })
  },
  toc(){
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/api/goods/collect?goods_id=' + this.data.items.goods_id + '&openid=' + app.globalData.openid,
      success:function(res){
        console.log(res)
        wx.showToast({
          title: res.data.msg,
        })
        that.getcdata()
      }
    })
  },
  getcdata(){
    var that = this;
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/api/goods/checkcollect?openid=' + app.globalData.openid,
      success:function(res){
        console.log(res.data.data)
        if (res.data.status == 0){
          let arr = [];
          for (var i = 0; i < res.data.data.length;i++){
            arr.push(res.data.data[i].goods_id)
          }
          let strArr = arr.toString();
          console.log('strArr',strArr)
          if (strArr.indexOf(that.data.items.goods_id) >= 0){
            that.setData({
              isc:true
            })
          }else{
            that.setData({
              isc: false
            })
          }
        }else{
          that.setData({
            isc: false
          })
        }
      }
    })
  }
})